<?php

namespace app\common\model\mysql;

use think\Model;

class SGoodsGroupLadder extends Model
{
    //批量插入拼团活动与拼团阶梯关联表数据
    public function insertAllGroupLadder($setGroupDataList,$goodsGroupId,$order_num = 0){
        $data = [];
        foreach ($setGroupDataList as $key=>$value) {
            $order_num++;
            $data[] = [
                'group_price'=> $value['groupPrice'],
                'goods_group_id' => $goodsGroupId,
                'group_people'=>$value['groupPeople'],
                'order_num'=> $order_num,
            ];
        }
        return $this->insertAll($data);
    }

    public function getGroupLadder($goodsGroupId){
        $order = [
            "order_num" =>"asc",
        ];
        $where = [
            ['goods_group_id', '=', $goodsGroupId]
        ];
        $result = $this->where($where)->order($order)->field("group_people as groupPeople,group_price as groupPrice")->select();
        return $result->toArray();
    }

    /**
     * 删除拼团活动与拼团阶梯数据信息
     * @param $ids
     * @return bool
     */
    public function deleteGroupLadderIds($id){
        if(empty($id)){
            return false;
        }
        $where = [
            ['goods_group_id', '=', $id]
        ];
        $result = $this->where($where)->delete();
        return $result;
    }

    /**
     * 获取拼团活动价格最低值
     * @param $goodsGroupId
     * @return mixed
     */
    public function getGroupLadderMinPrice($goodsGroupId){
        $where = [
            ['goods_group_id', '=', $goodsGroupId]
        ];
        $result = $this->where($where)->min('group_price');
        return $result;
    }
}
